package com.chapter11.chap_exercise.Journey_to_the_West;

//如果唐僧过火焰山就用飞机
public class Test06 {
    public static void main(String[] args) {
        Person Tang = new Person("唐僧", new Boat());
        Tang.Common();
        Tang.passRiver();
        Tang.Fire();

    }
}

@SuppressWarnings({"all"})
interface Vehicle {
    public abstract void work();
}

class Plane implements Vehicle {

    public void work(){
        System.out.println("坐着飞机穿过火焰山");
    }
}


class Horse implements Vehicle {

    public void work() {
        System.out.println("坐着马去西天取经");
    }
}

class Boat implements Vehicle {

    public void work() {
        System.out.println("坐着船去西天取经");
    }
}


@SuppressWarnings({"all"})

//交通工具工厂
class Factorial {//这里用static修饰是因为我们不想new一个Factorial对象

    private Factorial() { //第一步：构造器私有化
    }
    //第二步：创建私有静态对象并new一个,这里用接口接收
    private static Horse hourse = new Horse();

    //第三步创建公共方法
    public static Horse getHourse() {
        return hourse;
    }

    //这里稍后会进行优化,优化就是为了致敬经典中的白龙马，将此类设计成单例设计模式中的懒汉式
    /**
    public Horse getHorse() {
        return new Horse();
    }
    */

    //每次乘船被认为是新的船
    public static Boat getBoat() {
        return new Boat();
    }

    public static Plane getPlane() {
        return new Plane();
    }
}

@SuppressWarnings({"all"})
class Person {

    private String name;
    private Vehicle Vehicles;

    public Person(String name, Vehicle Vehicles) {
        this.name = name;
        this.Vehicles = Vehicles;
    }

    //写一个方法封装人过河或者赶路这个过程
    public void passRiver() {
        //1.因为从Person类传入的交通工具没有被充分使用，因此修改优化一下代码
        //这两者均可
        /**
        Factorial.getBoat().work();

        Boat boat =  Factorial.getBoat();
        boat.work();
         */

        //这里是向上转型
        if (!(Vehicles instanceof Boat)) {
            Vehicles = Factorial.getBoat();
        }
        Vehicles.work();
    }

    public void Common() {

        //这里是最初版本：先从交通工具入手

        //这两者均可
        /**
        Factorial.getHourse().work();

        Horse horse =  Factorial.getHourse();
        horse.work();
         */


        //这里提高了代码复用率，
        if (!(Vehicles instanceof Horse)) {
            Vehicles = Factorial.getHourse();
        }
        Vehicles.work();
    }



    public void Fire(){
        if (!(Vehicles instanceof Plane)) {
            Vehicles = Factorial.getPlane();
        }
        Vehicles.work();
    }

}


